package com.iqiyi.pps.epg.core.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.iqiyi.kiwi.utils.Configuration;
import com.iqiyi.pps.epg.core.service.sginal.SginalLogServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by zhang on 2016/3/30.
 */
public final class ApiSginalServerStatusUtil {

    private static Logger logger = LoggerFactory.getLogger(ApiSginalServerStatusUtil.class);

    private ApiSginalServerStatusUtil() {

    }

    public static final String SGINAL_SERVER_STATUS_LOAD = Configuration.getString("sginal.server.status.load.url");

    public static final String RES_SUCCESS = "A00000";

    public static String loadOutParams(String ip) {
        JSONObject params = new JSONObject();
        JSONArray queries = new JSONArray();
        // outside icmploss
        JSONObject object = new JSONObject();
        object.put("metric", "icmppingloss");
        JSONObject icmplossJSONObject = new JSONObject();
        icmplossJSONObject.put("ip", ip);
        icmplossJSONObject.put("a", ip);
        object.put("tags", icmplossJSONObject);
        queries.add(object);

        params.put("queries", queries);
        return JSON.toJSONString(params);
    }

    public static String loadParams(String ip) {
        JSONObject params = new JSONObject();
        JSONArray queries = new JSONArray();


        //load
        JSONObject object1 = new JSONObject();
        object1.put("metric", "system.cpu.load");
        JSONObject loadJSONObject = new JSONObject();
        loadJSONObject.put("ip", ip);
        object1.put("tags", loadJSONObject);
        queries.add(object1);

        //data free
        JSONObject object2 = new JSONObject();
        object2.put("metric", "vfs.fs.size");
        JSONObject dataFreeJSONObject = new JSONObject();
        dataFreeJSONObject.put("ip", ip);
        dataFreeJSONObject.put("a", "/data");
        dataFreeJSONObject.put("b", "free");
        object2.put("tags", dataFreeJSONObject);
        queries.add(object2);

        //data used
        JSONObject object3 = new JSONObject();
        object3.put("metric", "vfs.fs.size");
        JSONObject dataUsedJSONObject = new JSONObject();
        dataUsedJSONObject.put("ip", ip);
        dataUsedJSONObject.put("a", "/data");
        dataUsedJSONObject.put("b", "used");
        object3.put("tags", dataUsedJSONObject);
        queries.add(object3);

        // / used
        JSONObject object4 = new JSONObject();
        object4.put("metric", "vfs.fs.size");
        JSONObject allUsedJSONObject = new JSONObject();
        allUsedJSONObject.put("ip", ip);
        allUsedJSONObject.put("a", "/");
        allUsedJSONObject.put("b", "used");
        object4.put("tags", allUsedJSONObject);
        queries.add(object4);

        // / free
        JSONObject object5 = new JSONObject();
        object5.put("metric", "vfs.fs.size");
        JSONObject allFreeJSONObject = new JSONObject();
        allFreeJSONObject.put("ip", ip);
        allFreeJSONObject.put("a", "/");
        allFreeJSONObject.put("b", "free");
        object5.put("tags", allFreeJSONObject);
        queries.add(object5);

        // 可用内存
        JSONObject object6 = new JSONObject();
        object6.put("metric", "vm.memory.size");
        JSONObject availableMemJSONObject = new JSONObject();
        availableMemJSONObject.put("ip", ip);
        availableMemJSONObject.put("a", "available");
        object6.put("tags", availableMemJSONObject);
        queries.add(object6);

        // 总内存
        JSONObject object7 = new JSONObject();
        object7.put("metric", "vm.memory.size");
        JSONObject allMemJSONObject = new JSONObject();
        allMemJSONObject.put("ip", ip);
        allMemJSONObject.put("a", "empty_bracket");
        object7.put("tags", allMemJSONObject);
        queries.add(object7);

        // 总swap
        JSONObject object8 = new JSONObject();
        object8.put("metric", "system.swap.size");
        JSONObject allSwapJSONObject = new JSONObject();
        allSwapJSONObject.put("ip", ip);
        allSwapJSONObject.put("b", "total");
        object8.put("tags", allSwapJSONObject);
        queries.add(object8);

        // free swap
        JSONObject object9 = new JSONObject();
        object9.put("metric", "system.swap.size");
        JSONObject freeSwapJSONObject = new JSONObject();
        freeSwapJSONObject.put("ip", ip);
        freeSwapJSONObject.put("b", "pfree");
        object9.put("tags", freeSwapJSONObject);
        queries.add(object9);

        // inside icmploss
        JSONObject object10 = new JSONObject();
        object10.put("metric", "icmppingloss");
        JSONObject icmplossJSONObject = new JSONObject();
        icmplossJSONObject.put("ip", ip);
        icmplossJSONObject.put("a", ip);
        object10.put("tags", icmplossJSONObject);
        queries.add(object10);

        params.put("queries", queries);
        return JSON.toJSONString(params);
    }

    public static JSONArray getLoad(String ip) {
        JSONArray jsonArray = null;
        try {
            String url = SGINAL_SERVER_STATUS_LOAD;
            Map<String, String> hearderMap = new HashMap<>();
            hearderMap.put("postman-token", "bcc54c09-3c6d-2cbd-86de-a1b2d27587ae");
            jsonArray = ApiHttpRequestUtil.doPostRawJSONArray(url, loadParams(ip), hearderMap);

        } catch (Exception e) {
            logger.error("[ServerStatus][getLoad][Error]", e);
        }
        return jsonArray;
    }

    public static JSONArray getoutLoad(String ip) {
        JSONArray jsonArray = null;
        try {
            String url = SGINAL_SERVER_STATUS_LOAD;
            Map<String, String> hearderMap = new HashMap<>();
            hearderMap.put("postman-token", "bcc54c09-3c6d-2cbd-86de-a1b2d27587ae");
            jsonArray = ApiHttpRequestUtil.doPostRawJSONArray(url, loadOutParams(ip), hearderMap);

        } catch (Exception e) {
            logger.error("[ServerStatus][getLoad][Error]", e);
        }
        return jsonArray;
    }

    public static void main(String[] args) {
        String params = ApiSginalServerStatusUtil.loadParams("10.1.1.0");
        System.out.print("params = " + params);
    }

}
